package com.liqw.crawler.crawler.processor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;
import java.util.Optional;

@Component
@Slf4j
public class AliFapaiPageProcessor extends AbstractPageProcessor implements InitializingBean {
    @Override
    public String getType() {
        return "ali";
    }

    private Site site = Site.me()
            .setCharset("UTF-8")
            .setRetryTimes(3)
            .setSleepTime(1000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
            .addHeader("Referer", "strict-origin-when-cross-origin") // 全局 Referer
            ;

    @Override
    public void process(Page page) {
        Selectable url = page.getUrl();
        log.info("正在解析url:{}", url.toString());
        Selectable currentPageSelectable = page.getHtml().css("body > div.sf-wrap > div.pagination.J_Pagination > span.current", "text");
        Integer currentPage = Integer.valueOf(Optional.ofNullable(currentPageSelectable.get()).orElse("0"));
        System.out.println("============================================================");
        log.info("currentPage={}", currentPage);

        String titleSelect = "a > div.header-section > p.title";
        String startPriceSelect = "a > div.info-section > p.price-current > span.value > em.pai-xmpp-current-price";
        String currentPriceSelect = "a > div.info-section > p.price-current > span.value > em.pai-xmpp-current-price";
        String evaluatePriceSelect = "a > div.info-section > p.price.price-assess > span.value > em:nth-child(2)";
        String shootingTimeSelect = "a > div.info-section > p.time.time-todo.pai-status-todo-show > span.value";

        List<Selectable> nodes = page.getHtml().css("body > div.sf-wrap > div.sf-content > div.sf-item-list > ul > li.pai-item").nodes();
        for (Selectable node : nodes) {
            System.out.println("============================================================");
            Selectable selectable = node.css(titleSelect, "text");
            Optional.ofNullable(selectable).ifPresent(v -> System.out.println("标题: " + v.get()));

            selectable = node.css(startPriceSelect, "text");
            Optional.ofNullable(selectable).ifPresent(v -> System.out.println("起拍价: " + v.get()));

            selectable = node.css(evaluatePriceSelect, "text");
            Optional.ofNullable(selectable).ifPresent(v -> System.out.println("评估价: " + v.get()));

            selectable = node.css(shootingTimeSelect, "text");
            Optional.ofNullable(selectable).ifPresent(v -> System.out.println("起拍时间: " + v.get()));
        }

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        Selectable totalPageSelectable = page.getHtml().css("body > div.sf-wrap > div.pagination.J_Pagination > span.page-skip > em", "text");
        Integer totalPage = Integer.valueOf(Optional.ofNullable(totalPageSelectable.get()).orElse("0"));
        if(currentPage < totalPage){
            Selectable nextPageSelectable = page.getHtml().css("body > div.sf-wrap > div.pagination.J_Pagination > a.next", "href");
            page.addTargetRequest(nextPageSelectable.get());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AbstractPageProcessorManager.addPageProcessor( this);
    }
}